package com.hlingsoft.cms.action.manage;


import com.hlingsoft.cms.entity.*;
import com.hlingsoft.cms.entity.vo.JsonVo;
import com.hlingsoft.cms.entity.vo.PageVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import java.util.*;


import java.util.List;

/**
 * Created by selbylei on 2015/5/8.
 */

@Controller
@RequestMapping("/manage/appUser")
public class ManageAppUserAction extends ManageBaseAction {




    /**
     * 用户列表
     *
     * @param pageNum
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/list.htm")
    public String index(
            @RequestParam(value = "p", defaultValue = "1") int pageNum,
            ModelMap modelMap) {
        PageVo<AppUser> pageVo = new PageVo<AppUser>(pageNum);
        pageVo.setRows(10);
        pageVo.basePath = "/manage/appUser/list.htm";
        pageVo.setList(appUserDao.getAllUserList(pageVo.getOffset(), pageVo.getRows()));
        pageVo.setCount(appUserDao.getUserListCount());
        modelMap.put("p", pageNum);
        modelMap.put("pageVo", pageVo);
        return "manage/appUser/list";
    }


    /**
     * 禁用用户
     *
     * @param appUserId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/delete.json")
    @ResponseBody
    public JsonVo<String> delete(@RequestParam(value = "appUserId") long appUserId,
                                 ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        AppUser appUser = appUserDao.getUserById(appUserId);
        if (appUser != null) {
            //删除相关动态
            List<News> newsList = newsDao.getNewsByAppUserIdWithNone(appUserId);
            if (newsList.size() > 0) {
                for (News news : newsList) {
                    news.setAuthorization(0);
                    newsDao.updateNews(news);
                }
            }

            //删除评论相关
            List<Comment> commentList = commentDao.getCommentByAppUserIdWithNone(appUserId);
            if (commentList.size() > 0) {
                for (Comment comment : commentList) {
                    comment.setAuthorization(0);
                    commentDao.updateComment(comment);
                }
            }

            //删除评论消息
            List<CommentMessage> commentMessageList = commentMessageDao.getCommentMessageByAppUserIdWithNone(appUserId);
            if (commentMessageList.size() > 0) {
                for (CommentMessage commentMessage : commentMessageList) {
                    commentMessage.setAuthorization(0);
                    commentMessageDao.updateCommentMessage(commentMessage);
                }
            }

            //删除收货地址
            List<Consignee> consigneeList = consigneeDao.getConsigneeByAppUserIdWithNone(appUserId);
            if (consigneeList.size() > 0) {
                for (Consignee consignee : consigneeList) {
                    consignee.setAuthorization(0);
                    consigneeDao.updateConsignee(consignee);
                }
            }

            //关注
            List<Focus> focusList = focusDao.getFocusByAppUserIdWithNone(appUserId);
            if (focusList.size()>0){
                for (Focus focus:focusList){
                    focus.setAuthorization(0);
                    focusDao.updateFocus(focus);
                }
            }

            //关注消息
            List<FocusMessage> focusMessageList= focusMessageDao.getFocusMessageByAppUserIdWithNone(appUserId);
            if (focusMessageList.size()>0){
                for (FocusMessage focusMessage:focusMessageList){
                    focusMessage.setAuthorization(0);
                    focusMessageDao.updateFocusMessage(focusMessage);
                }
            }

            //删除图片
            List<Image> imageList = imageDao.getImageByAppUserIdWithNone(appUserId);
            if (imageList.size()>0){
                for (Image image:imageList){
                    image.setAuthorization(0);
                    imageDao.updateImage(image);
                }
            }

            //删除点赞
            List<Likes> likesList = likesDao.getLikesByAppUserIdWithNone(appUserId);
            if (likesList.size()>0){
                for (Likes likes:likesList){
                    likes.setAuthorization(0);
                    likesDao.updateLikes(likes);
                }
            }

            //删除喜欢记录
           List<LikesMessage> likesMessageList = likesMessageDao.getLikesMessageByAppUserIdWithNone(appUserId);
            if (likesMessageList.size()>0){
                for (LikesMessage likesMessage:likesMessageList){
                    likesMessage.setAuthorization(0);
                    likesMessageDao.updateLikesMessage(likesMessage);
                }
            }

            //删除
            messageStatusDao.deleteMessageStatusByAppUserId(appUserId);

            //删除订单
            List<OrderInfo> orderInfoList = orderInfoDao.getOrderListByAppUserIdWithNone(appUserId);
            if (orderInfoList.size()>0){
                for (OrderInfo orderInfo:orderInfoList){
                    orderInfo.setAuthorization(0);
                    orderInfoDao.updateOrderInfo(orderInfo);
                }
            }

            //删除举报
            reportDao.deleteReportByAppUserId(appUserId);

            //设置达人
             startDao.deleteStartByAppUserIdWithNone(appUserId);

             List<Tags> tagsList = tagsDao.getTagsByAppUserIdWithNone(appUserId);
            if (tagsList.size()>0){
                for (Tags tags:tagsList){
                    tags.setAuthorization(0);
                    tagsDao.updateTags(tags);
                }
            }
        }

        appUser.setAuthorization(0);
        appUserDao.deleteUserById(appUserId);
        json.setResult(true);
        return json;
    }


    /**
     * 启用这个账号
     *
     * @param appUserId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/getOn.json")
    @ResponseBody
    public JsonVo<String> getOn(@RequestParam(value = "appUserId") long appUserId,
                                ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        appUserDao.getOnUserById(appUserId);
        json.setResult(true);
        return json;
    }


    /**
     * 通过达人审核
     *
     * @param appUserId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/DRCheck.json")
    @ResponseBody
    public JsonVo<String> DRCheck(@RequestParam(value = "appUserId") long appUserId,
                                  ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        AppUser appUser = appUserDao.getUserById(appUserId);
        appUser.setIsDR(2);
        appUserDao.updateUser(appUser);
        json.setResult(true);
        return json;
    }

    /**
     * 取消审核
     *
     * @param appUserId
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/DRCancel.json")
    @ResponseBody
    public JsonVo<String> DRCancel(@RequestParam(value = "appUserId") long appUserId,
                                   ModelMap modelMap) {
        JsonVo<String> json = new JsonVo<String>();
        AppUser appUser = appUserDao.getUserById(appUserId);
        appUser.setIsDR(0);
        appUserDao.updateUser(appUser);
        json.setResult(true);
        return json;
    }


    /**
     * 查询
     * @param appUserId
     * @param pageNum
     * @param modelMap
     * @return
     */
    @RequestMapping(value = "/search.htm")
    public String search(
            @RequestParam(value = "appUserId",required = false) Integer appUserId,
            @RequestParam(value = "name",required = false) String name,
            @RequestParam(value = "p", defaultValue = "1") int pageNum,
            ModelMap modelMap) {
        PageVo<AppUser> pageVo = new PageVo<AppUser>(pageNum);
        pageVo.setRows(10);
        pageVo.basePath = "/manage/appUser/list.htm";
        List<AppUser> appUserList = new ArrayList<AppUser>();
        if (appUserId!=null){
            appUserList = appUserDao.getUserByName(name);
        }else {
            appUserList.add(appUserDao.getUserById(appUserId));
        }
        pageVo.setList(appUserList);
        pageVo.setCount(appUserList.size());
        modelMap.put("p", pageNum);
        modelMap.put("pageVo", pageVo);
        return "manage/appUser/list";
    }


}
